﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace GetImg
{
    public partial class Form1 : Form
    {
        //起点Y坐标
        public int SY = 0;
        //起点X坐标
        public int SX = 0;

        public int eY = 0;

        public int eX = 0;
        //截图标记
        public int flag = 0;

        public Form1()
        {
            InitializeComponent();

        }

        public void test()
        {
            var str = @"D:\mypro\1.png";
            var file = File.Open(str, FileMode.Open);
            // privateBitmap srcBitmap = null;
            //var srcBitmap = (Bitmap)Bitmap.FromFile(file.Name, false);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "BMP Files(*.bmp)|*.bmp|JPG Files(*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files(*.*)|*.*";
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //pbxShowImage.ImageLocation = ofd.FileName;
                var bmp = new Bitmap(ofd.FileName);
                if (bmp == null)
                {
                    MessageBox.Show("加载图片失败!", "错误");
                    return;
                }
                pictureBox1.Image = bmp;
                ConvertTo8Byte(bmp);
                ofd.Dispose();
            }
        }


        public unsafe void ConvertTo8Byte(Bitmap img)
        {
            //R(54)G(60)B(102)
            //R(254)G(254)B(254)
            var standPColor = Color.FromArgb(54, 60, 102);
            byte[] result = new byte[img.Width * img.Height];
            int n = 0;
            ////起点Y坐标
            //var SY = 0;
            ////起点X坐标
            //var SX = 0;
            BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                           PixelFormat.Format24bppRgb);
            var bp = (byte*)data.Scan0.ToPointer();
            float RBG = 0.3F * 54 + 0.59F * 60 + 0.11F * 102;
            //R(254)G(254)B(254)
            float endRBG = 0.3F * 254 + 0.59F * 254 + 0.11F * 254;
            for (int i = 0; i != data.Height; i++)
            {
                for (int j = 0; j != data.Width; j++)
                {
                    //注意位图结构中RGB按BGR的顺序存储
                    result[n] = bp[i * data.Stride + j * 3];
                    n++;
                    //var B = bp[i * data.Stride + j + 2];
                    //var G = bp[i * data.Stride + j + 1];
                    //var R = bp[i * data.Stride + j];
                    //if (B.ToString() == "102" && G.ToString() == "60" && R.ToString() == "54")
                    //{
                    //    SY = i;
                    //    SX = j;
                    //    break;
                    //}
                    //0.3R+0.59G+0.11B

                    float value = 0.11F * bp[i * data.Stride + j * 3] + 0.59F * bp[i * data.Stride + j * 3 + 1] +
                                  0.3F * bp[i * data.Stride + j * 3 + 2];
                    //对比RGB 获取 图像起点
                    if (value == RBG)
                    {
                        SY = i;
                        SX = j;

                    }

                    //判断是否有白色圈，有则获取坐标
                    if (value == endRBG)
                    {
                        eY = i;
                        eX = j;
                    }

                }
            }
            img.UnlockBits(data);
            //img.Dispose();
            //return result;
            // ColorPalette palette = bit.Palette;
            //for (int i = 0; i != palette.Entries.Length; i++)
            //{
            //    palette.Entries[i] = Color.FromArgb(i, i, i);
            //}
            //bit.Palette = palette;
            //bit.Save(destFile, ImageFormat.Bmp);
            //img.Dispose();
            //bit.Dispose();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            eX = e.Location.X;
            eY = e.Location.Y;
            //MessageBox.Show(e.Location.ToString());
            //跳
            Jump();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //判断X,Y轴是否有数据
            if (eX == 0 && eY == 0)
            {
                MessageBox.Show("未获取到终点坐标");
            }
        }




        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //截取图片
            while (flag == 0)
            {


                var tempFileName = "1.png";
                Cmd("shell screencap -p /sdcard/" + tempFileName);
                Cmd("pull /sdcard/" + tempFileName);
                //放入imgBox
                if (System.IO.File.Exists(tempFileName))
                {
                    //pictureBox1.BackgroundImage = new Bitmap(tempFileName);
                    using (var temp = Image.FromFile(tempFileName))
                    {
                        pictureBox1.Image = new Bitmap(temp);
                    }
                    //if (multiplierX == 0)
                    //{
                    //    multiplierX = pictureBox1.Image.Width / (pictureBox1.Width + 0.00);
                    //    multiplierY = pictureBox1.Image.Height / (pictureBox1.Height + 0.00);
                    //}
                    GC.Collect();
                    if (System.IO.File.Exists(tempFileName))
                    {
                        try
                        {
                            System.IO.File.Delete(tempFileName);
                        }
                        catch
                        {

                        }
                    }

                }
                flag = 1;

            }
        }


        //执行adb命令
        public string Cmd(string Cmd)
        {

            Process p = new Process();

            p.StartInfo = new ProcessStartInfo()
            {

                FileName = @"E:\adb\adb.exe",

                Arguments = Cmd,//要执行的命令

                UseShellExecute = false,//拒绝使用系统自带的Shell

                RedirectStandardInput = true,//接受输入

                RedirectStandardOutput = true, //接受输出

                RedirectStandardError = true,//接受错误

                CreateNoWindow = true,//不创建窗口

            };

            p.Start();

            string s = p.StandardOutput.ReadToEnd();//读取输出

            p.WaitForExit();

            return s;
        }

        public void Jump()
        {

        }

    }
}
